package com.xinongbest.oa.service;

import com.xinongbest.oa.dao.RbacDao;
import com.xinongbest.oa.dao.UserDao;
import com.xinongbest.oa.entity.Node;
import com.xinongbest.oa.entity.User;
import com.xinongbest.oa.service.exception.BusinessException;
import com.xinongbest.oa.utils.MD5Utils;

import java.util.List;

/**
 * @author lemonwan
 */
public class UserService {

    private UserDao userDao = new UserDao();
    private RbacDao rbacDao = new RbacDao();

    /**
     * 用户登录信息校验
     *
     * @param username 用户名
     * @param password 密码
     * @return 返回用户信息
     */
    public User checkLogin(String username, String password) {
        User user = userDao.selectByUsername(username);
        if (null == user) {
            throw new BusinessException("L001", "用户不存在");
        }
        String md5 = MD5Utils.md5Digest(password, user.getSalt());
        if (!user.getPassword().equals(md5)) {
            throw new BusinessException("L002", "登录密码错误");
        }
        return user;
    }

    /**
     * 获取登录用户拥有的资源节点
     *
     * @param userId 用户ID
     * @return 用户节点列表
     */
    public List<Node> selectNodeByUserId(Long userId) {
        List<Node> nodeList = rbacDao.selectNodeByUserId(userId);
        return nodeList;
    }

}
